package pl.put.miasi.volleyball;

import java.io.File;

import javax.imageio.ImageIO;
import org.jbox2d.collision.CircleDef;
import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.common.XForm;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;
import org.jbox2d.dynamics.joints.RevoluteJointDef;
import processing.core.PImage;

public class B2Player {

	private World world;
	private Main main;
	private Volleyball volleyball;
	private boolean bluePlayer;
	private Vec2 relPosition;
	public boolean moving;
	
	public B2Player(Volleyball volleyball, boolean bluePlayer, Vec2 relPosition) {
		this.volleyball = volleyball;
		this.world = this.volleyball.m_world;
		this.main = this.volleyball.parent;
		this.bluePlayer = bluePlayer;
		this.relPosition = relPosition;
	}

	private Body corpsBody;
	private BodyDef corpsBodyDef;
	private PolygonDef corpsShapeDef;

	private Body racketBody;
	private BodyDef racketBodyDef;
	private PolygonDef racketShapeDef;

	private Body wheelBody;
	private BodyDef wheelBodyDef;
	private CircleDef wheelShapeDef;


	private Vec2 position;
	private float imageScale;
	private Vec2 anchor;
	
	public void movePlayer(Vec2 velocity) {
		corpsBody.setLinearVelocity(velocity);
	}
	
	public void moveRacket(float velocity) {
		racketBody.setAngularVelocity(velocity);
	}
	
	public Vec2 getPosition() {
		return corpsBody.getPosition();
	}
	
	public void initialize() {
		PImage wheelImage = null;
		try {
			if (bluePlayer)
				wheelImage = new PImage(ImageIO.read(new File(
						"images/wheel_blue.gif")));
			else
				wheelImage = new PImage(ImageIO.read(new File(
						"images/wheel_yellow.gif")));
			wheelImage.format = 2;
		} catch (Exception e) {
			e.printStackTrace();
		}

		PImage corpsImage = null;
		try {
			if (bluePlayer)
				corpsImage = new PImage(ImageIO.read(new File(
						"images/corps_blue.gif")));
			else
				corpsImage = new PImage(ImageIO.read(new File(
						"images/corps_yellow.gif")));
			corpsImage.format = 2;
		} catch (Exception e) {
			e.printStackTrace();
		}

		PImage armImage = null;
		try {
			if (bluePlayer)
				armImage = new PImage(ImageIO.read(new File(
						"images/arm_blue.gif")));
			else
				armImage = new PImage(ImageIO.read(new File(
						"images/arm_yellow.gif")));
			armImage.format = 2;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		imageScale = 1.0f / Settings.CS;
		
		/*
		 * Create player
		 */

		/*
		 * Wheel
		 */
		wheelShapeDef = new CircleDef();
		wheelShapeDef.density = Settings.PLAYER_DENSITY;
		wheelShapeDef.friction = Settings.PLAYER_FRICTION;
		wheelShapeDef.restitution = Settings.PLAYER_RESTITUTION;
		wheelShapeDef.radius = Settings.PLAYER_WHEEL;

		wheelBodyDef = new BodyDef();
		wheelBodyDef.allowSleep = false;
		
		position = new Vec2(relPosition.x + Settings.PLAYER_WHEEL_X, relPosition.y + Settings.PLAYER_WHEEL_Y);
		wheelBodyDef.position.set(position);

		wheelBody = world.createDynamicBody(wheelBodyDef);

		wheelBody.createShape(wheelShapeDef);

		wheelBody.setMassFromShapes();
		position = new Vec2(0.0f, 0.0f);
		volleyball.bindImage(wheelImage, position, 0.0f, imageScale, wheelBody);
		wheelBodyDef.userData = "wheel";
		
		/*
		 * Corps
		 */
		corpsShapeDef = new PolygonDef();
		position = new Vec2(relPosition.x + Settings.PLAYER_CORPS_X, relPosition.y + Settings.PLAYER_CORPS_Y);
		corpsShapeDef.setAsBox(Settings.PLAYER_CORPS_DX, Settings.PLAYER_CORPS_DY);
		
		corpsShapeDef.density = Settings.PLAYER_DENSITY;
		corpsShapeDef.restitution = Settings.PLAYER_RESTITUTION;
		corpsShapeDef.friction = Settings.PLAYER_FRICTION;

		corpsBodyDef = new BodyDef();
		corpsBodyDef.userData = "body";
		corpsBodyDef.fixedRotation = true;
		corpsBodyDef.position.set(position);

		corpsBodyDef.allowSleep = false;
		corpsBodyDef.isBullet = false;

		corpsBody = world.createDynamicBody(corpsBodyDef);
		corpsBody.createShape(corpsShapeDef);
		corpsBody.setMassFromShapes();
		
		position = new Vec2(0.0f, 0.0f);
		volleyball.bindImage(corpsImage, position, 0.0f, imageScale, corpsBody);

		/*
		 * Wheel joint
		 */
		RevoluteJointDef jointDef = new RevoluteJointDef();
		jointDef.collideConnected = false;		

		anchor = new Vec2(relPosition.x + Settings.PLAYER_WHEEL_ANCHOR_X, relPosition.y + Settings.PLAYER_WHEEL_ANCHOR_Y);
		jointDef.initialize(wheelBody, corpsBody, anchor);
		jointDef.enableMotor = moving;
		jointDef.motorSpeed = 2;
		jointDef.maxMotorTorque = 10;
		world.createJoint(jointDef);

		
		/*
		 * Racket
		 */
		racketShapeDef = new PolygonDef();
		position = new Vec2(relPosition.x + Settings.PLAYER_RACKET_X, relPosition.y + Settings.PLAYER_RACKET_Y);
		racketShapeDef.setAsBox(Settings.PLAYER_RACKET_DX, Settings.PLAYER_RACKET_DY);

		racketShapeDef.density = Settings.PLAYER_DENSITY;
		racketShapeDef.restitution = Settings.PLAYER_RACKET_RESTITUTION;
		racketShapeDef.friction = Settings.PLAYER_FRICTION;

		racketBodyDef = new BodyDef();
		racketBodyDef.userData = "racket";
		racketBodyDef.fixedRotation = false;
		racketBodyDef.allowSleep = false;
		racketBodyDef.isBullet = true;
		racketBodyDef.position.set(position);

		racketBody = world.createDynamicBody(racketBodyDef);

		racketBody.createShape(racketShapeDef);
		racketBody.setMassFromShapes();
		
		position = new Vec2(0.0f, 0.0f);
		volleyball.bindImage(armImage, position, 0.0f, imageScale, racketBody);


		/*
		 * Racket joint
		 */
		jointDef = new RevoluteJointDef();
		jointDef.collideConnected = false;
		
		anchor = new Vec2(relPosition.x + Settings.PLAYER_RACKET_ANCHOR_X, 
				relPosition.y + Settings.PLAYER_RACKET_ANCHOR_Y);
		jointDef.initialize(corpsBody, racketBody, anchor);
		/*
		 * jointDef2.enableMotor = true; jointDef2.motorSpeed = 2;
		 * jointDef2.maxMotorTorque = 10;
		 */
		world.createJoint(jointDef);

	}
}
